<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">管理员登录</div>
      <!-- 登录表单 -->
      <el-form
        status-icon
        :model="loginForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <!-- 用户名输入框 -->
        <el-form-item prop="adminName">
          <el-input
            v-model="loginForm.adminName"
            prefix-icon="el-icon-user-solid"
            placeholder="用户名"
            @keyup.enter.native="login"
          />
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="adminPassword">
          <el-input
            v-model="loginForm.adminPassword"
            prefix-icon="iconfont el-icon-mymima"
            show-password
            placeholder="密码"
            @keyup.enter.native="login"
          />
        </el-form-item>
      </el-form>
      <!-- 登录按钮 -->
      <el-button type="primary" @click="login">登录</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      loginForm: {
        adminName: "",
        adminPassword: ""
      },
      rules: {
        adminName: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        adminPassword: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const that = this;
          // eslint-disable-next-line no-undef
                //发送登录请求
                let param = new URLSearchParams();
                param.append("adminName", that.loginForm.adminName);
                param.append("adminPassword", that.loginForm.adminPassword);
          that.axios.post("/api/login", param).then(({ data }) => {
            if (data.flag) {
              // 登录后保存用户信息
              that.$store.commit("login", data.data);
            //  that.$message.success("登录成功");
              that.$router.push({ path: "/" });
            } else {
              that.$message.error(data.message);
            }
          });


        } else {
          return false;
        }
      });
    }
  }
};
</script>


